package peer.player2;

import net.miginfocom.swing.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jdesktop.jdic.desktop.DesktopException;

import peer.player.AutoSeekKeyThread;
import peer.player.SaveLastPlayThread;
import peer.player2.Player;
import peer.swtdesigner.SWTResourceManager;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.io.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.util.*;
import java.util.prefs.Preferences;

public class Player {
	static String media="";
	static Player player;
	static boolean isPlayOne=false;
	static boolean ins=false;
	static boolean autoPlay = false;
	static boolean master = false;
	static boolean one = true;
	static boolean embed = false;
	String comm = "StopAndClose";
	static DatagramSocket ss;
	static Thread daeThread = null;
	static boolean run = true;
	static boolean isPlay=false;
	static boolean imSeek = true;
	static boolean lockPaint = false;
	static double sel = 0;
	static Thread setProgressDelay;
	static JProgressBar progressBar;
	static JLabel label_1=new JLabel();
	static int pi = 0;
	static boolean progressDrag = false;
	static int lastPi = 0;
	static int lastOffset = 0;
	static int dsDrag = 0;
	static int lastSeekTo = 0;
	static peer.player2.AutoSeekKeyThread autoSeekThread;
	static long lastSeekTime = 0;
	static Thread labelPaintThread;
	static Thread progressDragMonit;
	static long lockPosition = 0;
	static boolean progressMove = false;
	static int playOffset = 0;
	static boolean isPause = false;
	static boolean seekForward = true;
	static JButton button_6;
	static JButton button_5;
	static JButton button_4;
	static JButton button_3;
	static JButton button_10;
	static JButton button_15;
	static JButton button_x1;
	static JButton button;
	static JButton button10;
	static JButton button_osd;
	static JSlider slider_1;
	static JLabel label_3;
	static boolean isFullScr = false;
	static DefaultListModel listModel;
	static JList shareList;
	static JFrame frame = new JFrame("Player");
	static Process proc = null;
	static KeyAdapter keyAdapter;
	static JToolBar toolBar_1;
	static JToolBar toolBar_2;
	static JScrollPane pane;
	static int osdlevel = 0;
	static int seekTime=0;
	static Thread t1;
	static Thread t2;
	static long hwnd;
	
	public static void main(String[] args) 
	{
		init();
//		if (args.length == 1)
//		{
//			ins = true;
//			isPlayOne = true;
//			player = new Player(args[0]);
//		} 
//		else 
//		{
//			player = new Player(media);
//		}
		//toMaster();
		//ini();
	}

	static void playFile() throws IOException
	{
		//checkUi();
		isPlayOne = true;Menus.AddPlayRecord("aa");
		unLock();
		//stopPlay();
		progressBar.setValue(0);
		playOffset = 0;
		label_1.setBounds(0, 0, 0, 0);
		label_1.repaint();media="F:\\Download\\dygzh.wmv";
		if (media != null) 
		{
			if (isPlay) {
				stopPlay();
			}
			//toMaster();
			button_5.setIcon(new ImageIcon("./application/skin/b2c.png"));
			button_5.setForeground(java.awt.Color.BLUE);
			button_4.setForeground(java.awt.Color.BLACK);
			isPlay = true;
			Preferences userPrefs = Preferences.userRoot().node("lightp2p");
			String mplayerPath = "";
			if (embed) {
				mplayerPath = userPrefs.get("mplayerpath", "");
			} else {
				mplayerPath = peer.Config.basePath + "mplayer/MPlayer.exe";
			}
			String media2 = parseUrl(media);
			File file = new File(media2);
			String[] cmd=new String[] {
					mplayerPath,
					"-identify", "-slave", "-wid",
					String.valueOf(hwnd), "-ao", "win32", "-colorkey",
					"0x010101", "-nokeepaspect", 
					"-osdlevel",String.valueOf(osdlevel),
					// "-cache",String.valueOf(1024*100),
					"-ss",String.valueOf(seekTime),
					"F:\\Download\\dygzh.wmv" };
			try
			{
				
				proc =Runtime.getRuntime().exec(cmd);
				final InputStream lOut1 = proc.getErrorStream();
				final InputStream lOut2 = proc.getInputStream();
				final Runnable lPrinter1 = new Runnable() {
					public void run() {
						try {
							final BufferedReader lReader = new BufferedReader(new InputStreamReader(lOut1));

							for (String l = lReader.readLine(); l != null & isPlay; l = lReader
							.readLine()) {
								// System.out.println("ERROR "+l);
							}
						} catch (Throwable t) {
							t.printStackTrace();
						}
					}
				};
				final Runnable lPrinter2 = new Runnable() {
					int i = 0;

					boolean b = true;

					boolean continuePlay = false;

					public void run() {
						try {
							final BufferedReader lReader = new BufferedReader(new InputStreamReader(lOut2));

							for (String l = lReader.readLine(); l != null & isPlay; l = lReader
							.readLine()) {
							//	System.out.println("*** "+l);
								
							}
						} catch (Throwable t) {
							t.printStackTrace();
						}
					}
				};
				t1 = new Thread(lPrinter1);
				t1.start();
				t2 = new Thread(lPrinter2);
				t2.start();
			}
			catch(IOException ex)
			{
				ex.printStackTrace();
			}
		}
	}
	
	static void init()
	{
//		peer.Config.addAutoReg();
//		checkUi();
//		peer.Config.setUseFolder(peer.Config.getUseFolder());
//		peer.Config.resetPAdv();
//		peer.Config.setAutoRun(peer.Config.getAutoRun());
//		new Thread(){
//			public void run(){
//				peer.Config.assFormat("lv");
//			}
//		}.start();
		
		setProgressDelay = new Thread() {
			public void run() {
				while (true) {
					if (progressBar != null) {
						if (isPlay) {
							if (imSeek) {
								if (!lockPaint) {
									progressBar.setValue((int) sel);
								}
							}
							imSeek = true;
						}
					}

					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		};
		setProgressDelay.start();
		labelPaintThread = new Thread() {
			public void run() {
				while (true) {
					if (progressBar != null & !progressMove) {
						int dsDrag2 = playOffset;
						if (lockPaint) {
							dsDrag2 = (int) lockPosition;
						}
						if (dsDrag2 != 0) {
							int d0 = dsDrag2 / 3600;
							int d1 = Math.abs(d0 * 60 - dsDrag2 / 60);
							int d2 = dsDrag2 % 60;
							StringBuffer str2 = new StringBuffer();
							str2.append("[");
							if (d0 < 10) {
								str2.append(0);
							}
							str2.append(d0);
							str2.append(":");
							if (d1 < 10) {
								str2.append(0);
							}
							str2.append(d1);
							str2.append(":");
							if (d2 < 10) {
								str2.append(0);
							}
							str2.append(d2);
							str2.append("]");
							label_1.setText(str2.toString());
							int x2 = (int) ((progressBar.getValue() / (double) progressBar
									.getMaximum()) * (double) progressBar
									.getSize().width);
							int x = 0;
							java.awt.FontMetrics metrics = label_1
							.getFontMetrics(label_1.getFont());
							int textH = metrics.getHeight(); // 字符串的高, 只和字体有关
							int textW = metrics.stringWidth(label_1.getText()); // 字符串的宽

							if (x2 + textW > progressBar.getWidth()) {
								x = x2 - textW;
								
							} else {
								x = x2;
							}
							label_1.setBounds(x, 0, textW, textH);
						}
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		};
		labelPaintThread.start();

		progressDragMonit = new Thread() {
			public void run() {
				while (true) {
					if (progressDrag) {
						// System.out.println("AAA"+e.getPoint().x+"BBB"+progressBar.getSize().width+"CCC"+percent+"RRR"+pi+"FFF"+pi%2);

						if ((pi % 1 == 0 & lastPi != pi & Math.abs(playOffset
								- lastOffset) > 10)
								| System.currentTimeMillis() - lastSeekTime > 200) {
							if (lastSeekTo != dsDrag) {
								seekto(dsDrag);
								lastOffset = playOffset;
								lastSeekTime = System.currentTimeMillis();
								lastSeekTo = dsDrag;
								lastPi = pi;
							}
						}
					}
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		progressDragMonit.start();
		autoSeekThread = new peer.player2.AutoSeekKeyThread();
		keyAdapter = new KeyAdapter() {
			int keyCode;

			public void keyPressed(KeyEvent arg0) {
				keyCode = arg0.keyCode;
				/*
				 * if(proc!=null){ System.out.println("dddddddddddddddddddd
				 * "+keyCode); PrintStream s = new
				 * PrintStream(proc.getOutputStream()); s.print("key_down_event
				 * "+keyCode+ "\n"); s.flush(); }
				 * 
				 */
				// System.out.println("dddddddddddddddddddd "+keyCode);g
				if (keyCode == SWT.ARROW_RIGHT) {
					if (isPlay) {
						seekForward = true;
						if (!autoSeekThread.isStart) {
							autoSeekThread.startSeek(true, 5);
						}
					}
				} else if (keyCode == SWT.ARROW_LEFT) {
					if (isPlay) {
						seekForward = false;
						if (!autoSeekThread.isStart) {
							autoSeekThread.startSeek(false, 5);
						}
					}
				} else if (keyCode == SWT.KEYPAD_CR | keyCode == SWT.CR
						| keyCode == SWT.F11) {
					//fullScreenSwitch();
				} else if (keyCode == 32) {
					pausePlay();
				} else if (keyCode == SWT.ARROW_UP) {
					if (isPlay) {
						seekForward = true;
						if (!autoSeekThread.isStart) {
							autoSeekThread.startSeek(true, 60);
						}
					}
				} else if (keyCode == SWT.ARROW_DOWN) {
					if (isPlay) {
						seekForward = true;
						if (!autoSeekThread.isStart) {
							autoSeekThread.startSeek(false, 60);
						}
					}
				} else if (keyCode == SWT.PAGE_UP) {
					if (isPlay) {
						seekForward = true;
						if (!autoSeekThread.isStart) {
							autoSeekThread.startSeek(true, 60 * 10);
						}
					}
				} else if (keyCode == SWT.PAGE_DOWN) {
					if (isPlay) {
						seekForward = true;
						if (!autoSeekThread.isStart) {
							autoSeekThread.startSeek(false, 60 * 10);
						}
					}
				} else if (keyCode == 111) {
					//changeOSD();
				} else if (keyCode == SWT.ESC) {
//					if (isFullScr) {
//					//	fullScreenSwitch();
//					}
				} else if (keyCode == 57) {
					//decressVolume();
				} else if (keyCode == 48) {
				//	incressVolume();
				}
//				if (isFullScr) {
//
//					hideThread.hideDelay(1000);
//				}
			}

			public void keyReleased(KeyEvent arg0) {
				if (autoSeekThread.isStart) {
					autoSeekThread.stopSeek();
				}
			}

		};
		toolBar_1 = new JToolBar();
		toolBar_1.setDoubleBuffered(false);
		toolBar_1.setFloatable(false);
		//toolBar_1.setLayout(new FlowLayout(FlowLayout.LEFT)); 
		toolBar_1.setBorderPainted(false);
		MigLayout MigLayout3 = new MigLayout("insets 0 0 0 0",
				"[]0[]0[]0[]0[]", "[center]");
		toolBar_1.setLayout(MigLayout3);
		button_15 = new JButton();

		button_15.setBorderPainted(false);
		button_15.setIconTextGap(0);
		button_15.setMargin(new Insets(0, 0, 0, 0));
		button_15.setIcon(new ImageIcon("./application/skin/b0.png"));
		if (!embed) {
			toolBar_1.add(button_15, "height ::18");
		}
		button_15.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				Menus.openUrl();
			}
			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}
		});
		final JButton button_7 = new JButton();
		button_7.setBorderPainted(false);
		button_7.setIconTextGap(0);
		button_7.setMargin(new Insets(0, 0, 0, 0));
		button_7.setIcon(new ImageIcon("./application/skin/b1.png"));
		if (!embed) {
			toolBar_1.add(button_7, "height ::18");
		}
		button_7.addMouseListener(new java.awt.event.MouseListener() 
		{
			public void mouseClicked(java.awt.event.MouseEvent e) 
			{
				openFileDialog();
			}
			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}
		});
		button_5 = new JButton();
		button_5.setBorderPainted(false);
		button_5.setIconTextGap(0);
		ImageIcon buttonIcon = new ImageIcon(
				"./application/skin/b2.png", "a background of button");
		button_5.setIcon(buttonIcon);
		button_5.addMouseListener(new java.awt.event.MouseListener() 
		{
			public void mouseClicked(java.awt.event.MouseEvent e) 
			{
				try
				{
					if (isPlay) 
					{
						pausePlay();
					} 
					else
					{
						playFile();
					}
				}
				catch(IOException ex)
				{
					ex.printStackTrace();
				}
			}
			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}
		});
		toolBar_1.add(button_5, "height ::18");
		button_4 = new JButton();
		button_4.setIconTextGap(0);
		button_4.setIcon(new ImageIcon("./application/skin/b3.png"));
		button_4.addMouseListener(new java.awt.event.MouseListener() {

			public void mouseClicked(java.awt.event.MouseEvent e)
			{
				stopPlay();
//				if (isFullScr) {
//					hideThread.hideDelay();
//				}
			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});
		button_4.setBorderPainted(false);
		button_4.setMargin(new Insets(0, 0, 0, 0));
		toolBar_1.add(button_4, "height ::18");
		JButton button_9 = new JButton();
		button_9.setIconTextGap(0);
		button_9.setIcon(new ImageIcon("./application/skin/b11.png"));
		button_9.addMouseListener(new java.awt.event.MouseListener() {

			public void mouseClicked(java.awt.event.MouseEvent e) 
			{
//				int i = shareList.getSelectedIndex();
//				if (i - 1 >= 0) {
//					shareList.setSelectedIndex(i - 1);
//				}
//				Object ob = shareList.getSelectedValue();
//				if (ob != null) {
//					media = ob.toString();
//					try {
//						//playNewFile();
//					} catch (IOException e1) {
//						e1.printStackTrace();
//					}
//				}

			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});
		button_9.setBorderPainted(false);
		button_9.setMargin(new Insets(0, 0, 0, 0));
		toolBar_1.add(button_9, "height ::18");
		JButton button_13 = new JButton();
		button_13.setIconTextGap(0);
		button_13.setIcon(new ImageIcon("./application/skin/b12.png"));
		button_13.setBorderPainted(false);
		button_13.setMargin(new Insets(0, 0, 0, 0));
		toolBar_1.add(button_13, "height ::18");
		progressBar = new JProgressBar();
		progressBar.setForeground(new java.awt.Color(0x99, 0xcc, 0xff));
		progressBar.setDoubleBuffered(true);
		progressBar.setString("");
		progressBar.setStringPainted(true);
		progressBar.setMaximum(9000);
		toolBar_1.add(progressBar, "width :3000:,height ::18");
		button_3 = new JButton();
		button_3.addMouseListener(new java.awt.event.MouseListener() {

			public void mouseClicked(java.awt.event.MouseEvent e) {
				//changeMute();
			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});
		button_3.setBorderPainted(false);
		button_3.setMargin(new Insets(0, 0, 0, 0));
		button_3.setIcon(new ImageIcon("./application/skin/b4.png"));
		toolBar_1.add(button_3, "height ::18");
		slider_1 = new JSlider();
		slider_1.setDoubleBuffered(false);
		slider_1.setValue(peer.Config.getVolume());
		slider_1.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if ((JSlider) e.getSource() == slider_1) {
					//setVolume(slider_1.getValue());
				}

			}
		});
		slider_1.setOpaque(false);
		slider_1.setPaintLabels(true);
		slider_1.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(java.awt.event.MouseEvent e) {
				if (isFullScr) {
					//hideThread.hideDelay();
				}
			}

			public void mouseDragged(java.awt.event.MouseEvent e) {
				if (isFullScr) {
					//hideThread.hideDelay();
				}
			}
		});
		toolBar_1.add(slider_1, "width 70::,height ::18");
		
		MigLayout layout= new MigLayout("insets 0 0 0 0","[left]","[][]");
		JPanel toolPane=new JPanel();
		toolPane.setLayout(layout);//toolBar_1.setSize(600,600);
		toolPane.add(toolBar_1,"wrap");
		
		//frame.getContentPane().add(BorderLayout.SOUTH,toolBar_1);
		toolBar_2 = new JToolBar();
		toolBar_2.setDoubleBuffered(false);
		toolBar_2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		toolBar_2.setFocusable(false);
		toolBar_2.setBorderPainted(false);
		toolBar_2.setFloatable(false);
		MigLayout MigLayout4 = new MigLayout("insets 0 0 0 0",
				"[]0[]0[]0[]0[]", "[center]");
		toolBar_2.setLayout(MigLayout4);
		final JButton button_1 = new JButton();
		button_1.setBorderPainted(false);
		button_1.setMargin(new Insets(0, 0, 0, 0));

		button_1.setText("关闭");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//peer.Config.setPBound(wShell.getBounds());
				close();
			}
		});
		toolBar_2.add(button_1,"height ::15");
		label_3 = new JLabel();
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setText(" 00:00:00 / 00:00:00");
		toolBar_2.add(label_3, "height ::15");
		button_10 = new JButton();
		button_10.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				
			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});

		button_10.setMargin(new Insets(0, 0, 0, 0));
		button_10.setBorderPainted(false);
		button_10.setIcon(new ImageIcon("./application/skin/b5.png"));
		toolBar_2.add(button_10, "height ::15");
		button_x1 = new JButton();
		button_x1.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				if (!isFullScr) {
//					canvasx1();
//					hideThread.hideDelay();
				}

			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});

		button_x1.setMargin(new Insets(0, 0, 0, 0));
		button_x1.setBorderPainted(false);
		button_x1.setIcon(new ImageIcon("./application/skin/b6.png"));
		toolBar_2.add(button_x1, "height ::15");
		button = new JButton();
		button.setMargin(new Insets(0, 0, 0, 0));
		button.setBorderPainted(false);
		button.setIcon(new ImageIcon("./application/skin/b7.png"));
		if (!embed) {
			toolBar_2.add(button, "height ::15");
		}

		button.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				if (!isFullScr) {
					//changeTop();

				} else {
					//hideThread.hideDelay();
				}

			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});
		button10 = new JButton();
		button10.setMargin(new Insets(0, 0, 0, 0));
		button10.setBorderPainted(false);
		button10.setIcon(new ImageIcon("./application/skin/b10.png"));
		if (!embed) {
			toolBar_2.add(button10, "height ::15");
		}

		button10.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				if (!isFullScr) 
				{
					
				}
			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});

		button_osd = new JButton();
		if (osdlevel == 0) {
			button_osd.setText("屏显");
			button_osd.setForeground(java.awt.Color.BLACK);
		} else {
			button_osd.setText("屏显" + osdlevel);
			button_osd.setForeground(java.awt.Color.BLUE);
		}

		button_osd.setMargin(new Insets(0, 0, 0, 0));
		button_osd.setBorderPainted(false);

		button_osd.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				//changeOSD();
				if (isFullScr) {
					//hideThread.hideDelay();
				}

			}

			public void mouseEntered(java.awt.event.MouseEvent e) {

			}

			public void mouseExited(java.awt.event.MouseEvent e) {

			}

			public void mousePressed(java.awt.event.MouseEvent e) {

			}

			public void mouseReleased(java.awt.event.MouseEvent e) {

			}

		});
		toolBar_2.add(button_osd,"height ::15");
		JButton button_ass = new JButton();
		button_ass.setText("关联");
		button_ass.setMargin(new Insets(0, 0, 0, 0));
		button_ass.setBorderPainted(false);
		toolBar_2.add(button_ass,"height ::15");
		
		toolPane.add(toolBar_2);
		frame.getContentPane().add(BorderLayout.SOUTH,toolPane);
		JPanel playerPanel=new JPanel();
		frame.getContentPane().add(BorderLayout.CENTER,playerPanel);
				
		try 
		{
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} 
		catch(Exception e) 
		{
			e.printStackTrace();
		}
		new Menus();
		frame.pack();
		frame.setSize(700,700);
		frame.addWindowListener(
	      	 	new WindowAdapter(){
	       	 	   public void windowClosing(WindowEvent e){
	       	 	      System.exit(0);
	       	 	   }
	       	    }
	          );
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(peer.Config.basePath+"skin/pl.gif"));
		int w = (Toolkit.getDefaultToolkit().getScreenSize().width - 800) / 2; 
		int h = (Toolkit.getDefaultToolkit().getScreenSize().height - 800) / 2; 
		
		Canvas f=new Canvas();
		f.setVisible(true);f.setSize(100,100);
		f.setBackground(new Color(0x1,0x1,0x1));
		JLayeredPane p=new JLayeredPane();p.add(f,JLayeredPane.FRAME_CONTENT_LAYER,-40000);
		frame.getContentPane().add(p,BorderLayout.CENTER);
		//frame.getLayeredPane().add(f,JLayeredPane.FRAME_CONTENT_LAYER);
		frame.setLocation(w,h);
		frame.setVisible(true);
		hwnd=JavaAccess.getHWND(f,System.getProperty("sun.boot.library.path")+"\\jawt.dll");
		
//		progressBar.setValue(0);
//		playOffset = 0;
//		media="http://172.16.1.43/dygzh.wmv";
//		//media="F:\\Download\\dygzh.wmv";
//		if (media != null) 
//		{
//			String mplayerPath = peer.Config.basePath + "mplayer/MPlayer.exe";
//			String[] cmd=new String[] {
//					mplayerPath,
//					"-identify", "-slave", "-wid",
//					String.valueOf(hwnd), "-ao", "win32", "-colorkey",
//					"0x010101", "-nokeepaspect", 
//					"-osdlevel",String.valueOf(osdlevel),
//					// "-cache",String.valueOf(1024*100),
//					"-ss",String.valueOf(seekTime),
//					media };
//			try
//			{
//				System.out.println("hwnd "+ String.valueOf(hwnd));
//				for(String s:cmd){
//					System.out.print(s+" ");
//				}
//				System.out.println("");
//				proc =Runtime.getRuntime().exec(cmd);
//				final InputStream lOut1 = proc.getErrorStream();
//				final InputStream lOut2 = proc.getInputStream();
//				final Runnable lPrinter1 = new Runnable() {
//					public void run() {
//						try {
//							final BufferedReader lReader = new BufferedReader(new InputStreamReader(lOut1));
//
//							for (String l = lReader.readLine(); l != null ; l = lReader
//							.readLine()) {
//								 //System.out.println("ERROR "+l);
//							}
//						} catch (Throwable t) {
//							t.printStackTrace();
//						}
//					}
//				};
//				final Runnable lPrinter2 = new Runnable() {
//					int i = 0;
//
//					boolean b = true;
//
//					boolean continuePlay = false;
//
//					public void run() {
//						try {
//							final BufferedReader lReader = new BufferedReader(new InputStreamReader(lOut2));
//
//							for (String l = lReader.readLine(); l != null & isPlay; l = lReader
//							.readLine()) {
//								System.out.println("*** "+l);
//								
//							}
//						} catch (Throwable t) {
//							t.printStackTrace();
//						}
//					}
//				};
//				t1 = new Thread(lPrinter1);
//				t1.start();
//				t2 = new Thread(lPrinter2);
//				t2.start();
//			}
//			catch(IOException ex)
//			{
//				ex.printStackTrace();
//			}
//		}
	}
	
	static void setVolume(int vol)
	{
//		if (!mute) 
//		{
			if (proc != null) {
				if (!isPause) {
					PrintStream s = new PrintStream(proc.getOutputStream());
					s.print("volume " + vol + " 1\n");
					s.flush();
				}
			}
		//}
	}

	static void incressVolume() 
	{
		int vol = slider_1.getValue() + 1;
		if (vol > slider_1.getMaximum()) {
			vol = slider_1.getMaximum();
		}
		slider_1.setValue(vol);
		if (proc != null) {
			if (!isPause) {
				PrintStream s = new PrintStream(proc.getOutputStream());
				s.print("volume " + vol + " 1\n");
				s.flush();
			}
		}
	}

	static void decressVolume() 
	{
		int vol = slider_1.getValue() - 1;
		if (vol < 0) {
			vol = 0;
		}
		slider_1.setValue(vol);
		if (proc != null) {
			if (!isPause) {
				PrintStream s = new PrintStream(proc.getOutputStream());
				s.print("volume " + vol + " 1\n");
				s.flush();
			}
		}
	}

	static void checkUi() {
		DatagramSocket ds = null;
		try {
			ds = new DatagramSocket(peer.Config.uiPort);
		} catch (SocketException e) {
			return;
		}
		ds.close();
		File file = new File("ui.exe");
		if (file.exists()) {
			try {
				org.jdesktop.jdic.desktop.Desktop.open(file);
			} catch (DesktopException e) {
				e.printStackTrace();
			}
		}

	}
	
	public static void unLock() {
		lockPaint = false;
	}
	static void pausePlay() {
		if (proc != null) {
			PrintStream s = new PrintStream(proc.getOutputStream());
			s.print("pause\n");
			s.flush();
			if (isPause) {
				isPause = false;
				button_5.setIcon(new ImageIcon("./application/skin/b2c.png"));
				//setVolume(slider_1.getValue());
			} else {
				button_5.setIcon(new ImageIcon("./application/skin/b2b.png"));
				isPause = true;
			}
		}
	}
	public static void seekto(int value) {
		if (proc != null) {
			PrintStream s = new PrintStream(proc.getOutputStream());
			String command = "seek " + value + " 2\n";
			s.print(command);
			s.flush();
			if (isPlay) {
				button_5.setIcon(new ImageIcon("./application/skin/b2c.png"));
			}

		}
	}
	
	public static void seekInt(int value)
	{
		if (proc != null) {
			PrintStream s = new PrintStream(proc.getOutputStream());
			String command = null;
			command = "seek " + value + " 0\n";

			s.print(command);
			s.flush();
			if (isPlay) {
				button_5.setIcon(new ImageIcon("./application/skin/b2c.png"));
			}

		}
	}
	
	static void toMaster()
	{
		if (!master) {
			try {
				ss = new DatagramSocket(peer.Config.daePort);
				startDeaServer();
			}
			catch (IOException e1)
			{
				master = false;
				try {
					InetAddress ip = InetAddress.getByName("127.0.0.1");
					DatagramSocket jj = null;
					try {
						jj = new DatagramSocket();
						byte[] b = "isEmbed".getBytes();
						DatagramPacket dp = new DatagramPacket(b, b.length);
						dp.setAddress(ip);
						dp.setPort(peer.Config.daePort);
						jj.send(dp);

						byte[] b2 = new byte[1024];
						DatagramPacket dp2 = new DatagramPacket(b2, b2.length);
						jj.receive(dp2);
						int length = dp2.getLength();
						String str = new String(b2, 0, length);
						if (str.equals("true")) {
							byte[] b3 = "stopAndClose".getBytes();
							DatagramPacket dp3 = new DatagramPacket(b3,
									b3.length);
							dp3.setAddress(ip);
							dp3.setPort(peer.Config.daePort);
							jj.send(dp3);
						} else if (str.equals("false")) {
							if (embed) {
								byte[] b3 = "stopAndClose".getBytes();
								DatagramPacket dp3 = new DatagramPacket(b3,
										b3.length);
								dp3.setAddress(ip);
								dp3.setPort(peer.Config.daePort);
								jj.send(dp3);
							} else {
								if (media != null) {
									byte[] b3 = ("play " + media).getBytes();
									DatagramPacket dp3 = new DatagramPacket(b3,
											b3.length);
									dp3.setAddress(ip);
									dp3.setPort(peer.Config.daePort);
									jj.send(dp3);
									System.exit(0);
								}
								close();
							}
						}
					} catch (IOException e) {
						// e.printStackTrace();
						if (jj != null) {
							jj.close();
						}
					}
				} catch (UnknownHostException e2) {
					// e2.printStackTrace();
				}
				try {
					Thread.sleep(500);
					ss = new DatagramSocket(peer.Config.daePort);
					startDeaServer();
				}
				catch (IOException e) 
				{
					// e.printStackTrace();
				}
				catch (InterruptedException e) 
				{
					// TODO 自动生成 catch 块
					 e.printStackTrace();
				}
			}
		}
	}

	static void close() 
	{
			daeThread.interrupt();
			ss.close();
			run = false;
			
			System.exit(0);
	}
	
	static void stopPlay() {
//		chipInfo = "";
//		hasPeerAdv = false;
		
		unLock();
		sel = 0;
		playOffset = 0;
		progressBar.setValue(0);
		label_1.setVisible(false);
		if (isPlay) {
			isPlay = false;
			label_3.setText(" 00:00:00 / 00:00:00");
			button_5.setForeground(java.awt.Color.BLACK);
			button_5.setIcon(new ImageIcon("./application/skin/b2.png"));
			button_4.setForeground(java.awt.Color.BLUE);
			button_6.setForeground(java.awt.Color.BLACK);
			if (proc != null) {
				PrintStream s = new PrintStream(proc.getOutputStream());
				s.print("quit\n");
				s.flush();
				progressBar.setValue(0);
				proc.destroy();
			}
			if (t1 != null) {
				t1.interrupt();
			}
			if (t2 != null) {
				t2.interrupt();
			}
		}
		
	}
		
	static void startDeaServer() 
	{
//		daeThread = null;
//		daeThread = new Thread() {
//			public void run() {
//				master = true;
//				Socket s = null;
//				try {
//					while (run) {
//						byte[] b = new byte[1024];
//						DatagramPacket dp = new DatagramPacket(b, b.length);
//						ss.receive(dp);
//						InetAddress ip = dp.getAddress();
//						int port = dp.getPort();
//						int length = dp.getLength();
//						String str = new String(b, 0, length);
//						System.out.println("--"+str+"==");
//						if (str.equals("isEmbed")) {
//							if (embed) {
//								dp.setData("true".getBytes());
//							} else {
//								dp.setData("false".getBytes());
//							}
//							dp.setAddress(ip);
//							dp.setPort(port);
//							ss.send(dp);
//						} else if (str.equals("stopAndClose")) {
//							master = false;
//							stopPlay();
//							ss.close();
//							break;
//						} else if (str.startsWith("play")) {
//							String str2 = str.substring(5);
//							if (str2.startsWith("p2pvod")) {
//								String str3 = "http";
//								str2 = str3 + str2.substring(6);
//							}
//							media = str2;
//							setVideoFile(media);
//							isPlayOne = true;
//
//							playStart(str2);
//
//						}
//					}
//				} catch (IOException e) {
//					notOne();
//				} finally {
//					if (s != null) {
//						try {
//							s.close();
//						} catch (IOException e) {
//							// e.printStackTrace();
//						}
//					}
//
//				}
//			}
//		};
//		daeThread.start();
    }
	
	static void notOne() 
	{
		one = false;
	}
	
	static void setVideoFile(String str)
	{
		media = str;
	}
	
	static String parseUrl(String media)
	{
		String media2 = new String(media);
		if (media.startsWith("lightvod:")) {
			media2 = media.substring(11);
		}
		else
		{
			File file=new File(media);
			if(file.exists()){
				if(file.getName().endsWith(".lv"))
				{
					try {
						FileReader reader=new FileReader(file);
						BufferedReader bReader=new BufferedReader(reader);
						String l=bReader.readLine();
						if(l!=null){
							media2=parseUrl(l);
						}
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}

				}
			}
		}
		return media2;
	}
	
	Player(final String VIDEO_FILE)
	{
		player = this;
		this.media = VIDEO_FILE;
//		wDisplay = new Display();
//		wShell = new Shell(SWT.SHELL_TRIM);
//		//wShell.setMinimumSize(400, 300);
//		wShell.setImage(SWTResourceManager.getImage(peer.Config.basePath
//				+ "skin/pl.gif"));
	}

	static void openFileDialog()
	{
		JFileChooser chooser=new JFileChooser();
		chooser.setMultiSelectionEnabled(true);
		int result=chooser.showOpenDialog(frame);
		if(result==JFileChooser.APPROVE_OPTION)
		{
			File[] files=chooser.getSelectedFiles();
			if(files.length > 1)
			{
				
			}
			else if(files.length==1)
			{
				JOptionPane.showMessageDialog(null,files[0].getName());
			}
		}
	}
}

